package com.zhuanzhuan.hero.user.helper;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.bj58.youpin.common.utils.json.JsonUtils;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ServiceHeaderEntity;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ZZOpenScfBaseResult;
import com.google.common.collect.Lists;
import com.zhuanzhuan.hero.biz.common.util.WxWarnTemplateUtil;
import com.zhuanzhuan.hero.recycle.order.facade.ICommonFacade;
import com.zhuanzhuan.hero.recycle.order.vo.BrandAndModelVO;
import com.zhuanzhuan.hero.user.consts.Consts;
import com.zhuanzhuan.hero.user.pojo.bo.BrandBO;
import com.zhuanzhuan.hero.user.pojo.bo.ModelBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName PanguHelper
 * @Description
 * @Author hanjunjun
 * @Date 2024/6/25 16:18
 */
@Component
@Slf4j
public class PanguHelper {

    @Resource
    private ICommonFacade commonFacade;

    /**
     * 根据品类id获取所有品牌
     *   查不到则返回空数组
     * @param cateId
     * @return java.util.List<com.zhuanzhuan.hero.user.pojo.bo.BrandBO>
     * @author  hanjunjun
     * @date 2024/6/25
     **/
    public List<BrandBO> getAllBrandByCate(Integer cateId) {
        log.info("getAllBrandByCate cateId={}", cateId);
        ZZOpenScfBaseResult<List<BrandAndModelVO>> result =
            commonFacade.getAllBrand(new ServiceHeaderEntity(), cateId);
        log.info("getAllBrandByCate result={}", JsonUtils.toJsonWithoutNull(result));
        if(!result.isSuccess()){
            WxWarnTemplateUtil.warnOutInvokedService(Consts.SERVICE_NAME, Consts.RECYCLE_ORDER_SERVICE_NAME, "getAllBrand调用失败");
            return Lists.newArrayList();
        }

        List<BrandAndModelVO> voList = result.getData();
        if(CollectionUtils.isEmpty(voList)){
            return Lists.newArrayList();
        }
        return voList.stream()
            .map(brandBean -> new BrandBO(Integer.valueOf(brandBean.getBrandId()), brandBean.getName()))
            .collect(Collectors.toList());
    }

    /**
     * 根据品类、品牌查机型
     *
     * @param cateId
     * @param brandId
     * @return java.util.List<com.zhuanzhuan.hero.user.pojo.bo.ModelBO>
     * @author  hanjunjun
     * @date 2024/6/26
     **/
    public List<ModelBO> getAllModelByCateAndBrand(Integer cateId, Integer brandId) {
        log.info("getAllModelByCateAndBrand cateId={} brandId={}", cateId, brandId);
        ZZOpenScfBaseResult<List<BrandAndModelVO>> result =
            commonFacade.getModelByBrand(new ServiceHeaderEntity(), cateId, brandId);
        log.info("getAllModelByCateAndBrand result={}", JsonUtils.toJsonWithoutNull(result));
        if(!result.isSuccess()){
            WxWarnTemplateUtil.warnOutInvokedService(Consts.SERVICE_NAME, Consts.RECYCLE_ORDER_SERVICE_NAME, "getModelByBrand调用失败");
            return Lists.newArrayList();
        }

        List<BrandAndModelVO> voList = result.getData();
        if(CollectionUtils.isEmpty(voList)){
            return Lists.newArrayList();
        }

        return voList.stream()
            .map(modelBean -> new ModelBO(Integer.valueOf(modelBean.getModelId()), modelBean.getName()))
            .collect(Collectors.toList());
    }
}
